package com.leps.nq.op.controller;

import com.leps.nq.bu.entity.BuWell;
import com.leps.nq.bu.service.IBuWellService;
import com.leps.nq.common.aspect.Log;
import com.leps.nq.common.enums.BusinessType;
import com.leps.nq.common.vo.PageVo;
import com.leps.nq.common.vo.R;
import com.leps.nq.common.vo.RBuilder;
import com.leps.nq.op.entity.OpInspection;
import com.leps.nq.op.service.IOpInspectionService;
import com.leps.nq.op.vo.InspectionQueryModel;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 巡检日志表 前端控制器
 * </p>
 *
 * @author hex
 * @since 2022-05-22
 */
@RestController
@RequestMapping("/op/op-inspection")
@Api(tags = "巡检日志管理")
public class OpInspectionController {
    @Autowired
    IOpInspectionService inspectionService;
    @Autowired
    IBuWellService wellService;

    @GetMapping
    @ApiOperation("巡检日志列表")
    public R<PageVo<OpInspection>> list(InspectionQueryModel model) {
        PageVo<OpInspection> pageVo = this.inspectionService.pageByQueryModel(model);
        return RBuilder.ok(pageVo);
    }

    @GetMapping("/{id}")
    @ApiOperation("根据id查询巡检日志")
    public R<OpInspection> getById(@PathVariable Long id) {
        return RBuilder.ok(this.inspectionService.getById(id));
    }

    @PostMapping
    @ApiOperation("新增巡检日志")
    @Log(title = "巡检日志", businessType = BusinessType.INSERT)
    public R<?> save(@RequestBody OpInspection inspection) {
        boolean save = this.inspectionService.save(inspection);
        // 更新相关井最近一次巡检信息
        BuWell well = new BuWell();
        well.setWellId(inspection.getWellId());
        well.setLastProcessTypeChange(inspection.getProcessTypeChange());
        this.wellService.updateById(well);
        return RBuilder.ofBoolean(save);
    }

    @PutMapping
    @ApiOperation("修改巡检日志")
    @Log(title = "巡检日志", businessType = BusinessType.UPDATE)
    public R<?> update(@RequestBody OpInspection inspection) {
        this.inspectionService.updateById(inspection);
        // 更新相关井最近一次巡检信息
        BuWell well = new BuWell();
        well.setWellId(inspection.getWellId());
        well.setLastProcessTypeChange(inspection.getProcessTypeChange());
        this.wellService.updateById(well);
        return RBuilder.ok();
    }

    @DeleteMapping
    @ApiOperation("删除巡检日志")
    @Log(title = "巡检日志", businessType = BusinessType.DELETE)
    public R<?> remove(@RequestBody List<Long> inspectionIds) {
        boolean b = this.inspectionService.removeBatchByIds(inspectionIds);
        return RBuilder.ofBoolean(b);
    }
}
